package com.walkline.weibo.dao;

import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import org.json.me.JSONObject;
import com.walkline.util.StringUtility;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.User;

public class WeiboUser extends WeiboObject implements User
{
	private long _id = 0;
	private String _id_str = "";
	private String _screen_name = "";
	private String _name = "";
	private int _province = 0;
	private int _city = 0;
	private String _location = "";
	private String _description = "";
	private String _url = "";
	private String _profile_image_url = "";
	private String _profile_url = "";
	private String _domain = "";
	private String _weihao = "";
	private String _gender = "";
	private int _followers_count = 0;
	private int _friends_count = 0;
	private int _statuses_count = 0;
	private int _favourites_count = 0;
	private Date _created_at = new Date();
	private boolean _following = false;
	private boolean _allow_all_act_msg = false;
	private boolean _geo_enabled = false;
	private boolean _verified = false;
	private int _verified_type = 0;
	private String _remark = "";
	private Status _status = null;
	private boolean _allow_all_comment = false;
	private String _avatar_large = "";
	private String _avatar_hd = "";
	private String _verified_reason = "";
	private boolean _follow_me = false;
	private int _online_status = 0;
	private int _bi_followers_count = 0;
	private String _language = "";

	private byte[] _avatar_data = null;
	private byte[] _avatar_data_hd = null;

	public WeiboUser(WeiboSDK weibo, JSONObject jsonObject) throws WeiboException
	{
		super(weibo, jsonObject);

		JSONObject user = jsonObject;
		if (user != null)
		{
			_id = user.optLong("id");
			_id_str = user.optString("idstr");
			_screen_name = user.optString("screen_name");
			_name = user.optString("name");
			_province = user.optInt("province");
			_city = user.optInt("city");
			_location = user.optString("location");
			_description = user.optString("description");
			_url = user.optString("url");
			_profile_image_url = user.optString("profile_image_url");
			_profile_url = user.optString("profile_url");
			_domain = user.optString("domain");
			_weihao = user.optString("weihao");
			_gender = getUserGender(user.optString("gender"));
			_followers_count = user.optInt("followers_count");
			_friends_count = user.optInt("friends_count");
			_statuses_count = user.optInt("statuses_count");
			_favourites_count = user.optInt("favourites_count");
			_created_at = StringUtility.parseDate(user.optString("created_at"));
			_following = user.optBoolean("following");
			_allow_all_act_msg = user.optBoolean("allow_all_act_msg");
			_geo_enabled = user.optBoolean("geo_enabled");
			_verified = user.optBoolean("verified");
			_verified_type = user.optInt("verified_type");
			_remark = user.optString("remark");

			if (!user.isNull("status")) {_status = new WeiboStatus(weibo, user.optJSONObject("status"));}

			_allow_all_comment = user.optBoolean("allow_all_comment");
			_avatar_large = user.optString("avatar_large");
			_avatar_hd = user.optString("avatar_hd");
			_verified_reason = user.optString("verified_reason");
			_follow_me = user.optBoolean("follow_me");
			_online_status = user.optInt("online_status");
			_bi_followers_count = user.optInt("bi_followers_count");
			_language = user.optString("lang");
		}
	}

	/**
	 * 用户UID
	 */
	public long getId() {return _id;}

	/**
	 * 字符串型的用户UID
	 */
	public String getIdStr() {return _id_str;}

	/**
	 * 用户昵称
	 */
	public String getScreenName() {return _screen_name;}

	/**
	 * 友好显示名称
	 */
	public String getName() {return _name;}

	/**
	 * 用户所在省级ID
	 */
	public int getProvince() {return _province;}

	/**
	 * 用户所在城市ID
	 */
	public int getCity() {return _city;}

	/**
	 * 用户所在地
	 */
	public String getLocation() {return _location;}

	/**
	 * 用户个人描述
	 */
	public String getDescription() {return _description;}

	/**
	 * 用户博客地址
	 */
	public String getBlogUrl() {return _url;}

	/**
	 * 用户头像地址（中图），50×50像素
	 */
	public String getProfileImageUrl() {return _profile_image_url;}

	/**
	 * 用户的微博统一URL地址
	 */
	public String getProfileUrl() {return _profile_url;}

	/**
	 * 用户的个性化域名
	 */
	public String getDomain() {return _domain;}

	/**
	 * 用户的微号
	 */
	public String getWeihao() {return _weihao;}

	/**
	 * 用户的性别
	 */
	public String getGender() {return _gender;}

	/**
	 * 粉丝数
	 */
	public int getFollowersCount() {return _followers_count;}

	/**
	 * 关注数
	 */
	public int getFriendsCount() {return _friends_count;}

	/**
	 * 微博数
	 */
	public int getStatusesCount() {return _statuses_count;}

	/**
	 * 收藏数
	 */
	public int getFavouritesCount() {return _favourites_count;}

	/**
	 * 是否已关注
	 */
	public boolean isFollowing() {return _following;}

	/**
	 * 是否允许所有人给我发私信
	 */
	public boolean isAllowAllActMsg() {return _allow_all_act_msg;}

	/**
	 * 是否允许标识用户的地理位置
	 */
	public boolean isGeoEnabled() {return _geo_enabled;}

	/**
	 * 是否是微博认证用户，即加V用户
	 */
	public boolean isVerified() {return _verified;}

	/**
	 * 用户认证类型
	 */
	public int getVerifiedType() {return _verified_type;}

	/**
	 * 用户备注信息，只有在查询用户关系时才返回此字段
	 */
	public String getRemark() {return _remark;}

	/**
	 * 用户的最近一条微博信息字段
	 */
	public Status getLastStatus() {return _status;}

	/**
	 * 是否允许所有人对我的微博进行评论
	 */
	public boolean isAllowAllComment() {return _allow_all_comment;}

	/**
	 * 用户头像地址（大图），180×180像素
	 */
	public String getAvatarLarge() {return _avatar_large;}

	/**
	 * 用户头像地址（高清），高清头像原图
	 */
	public String getAvatarHd() {return _avatar_hd;}

	/**
	 * 认证原因
	 */
	public String getVerifiedReason() {return _verified_reason;}

	/**
	 * 该用户是否关注当前登录用户
	 */
	public boolean isFollowMe() {return _follow_me;}

	/**
	 * 用户的在线状态
	 */
	public int getOnlineStatus() {return _online_status;}

	/**
	 * 用户的互粉数
	 */
	public int getBiFollowersCount() {return _bi_followers_count;}

	/**
	 * 用户当前的语言版本
	 */
	public String getLanguage() {return _language;}

	/**
	 * 用户创建（注册）时间 (长日期)
	 * @return 格式为: "yyyy-MM-dd HH:mm"
	 */
	public String getCreatedAtLong()
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 用户创建（注册）时间 (短日期)
	 * @return 格式为: "M/d HH:mm"
	 */
	public String getCreatedAtShort()
	{
		SimpleDateFormat sdf=new SimpleDateFormat("M/d HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 取得中文性别
	 * @return 中文性别，包括 男、女和未知
	 */
	private String getUserGender(String gender)
	{
		String sex = "未知";
		
		if (gender.equalsIgnoreCase("m"))
		{
			sex="男";
		} else if (gender.equalsIgnoreCase("f")) {
			sex="女";
		}

		return sex;
	}

	public void setAvatarIconData(byte[] data) {_avatar_data = data;}

	public byte[] getAvatarIconData() {return _avatar_data;}

	public void setAvatarHdIconData(byte[] data) {_avatar_data_hd = data;}

	public byte[] getAvatarHdIconData() {return _avatar_data_hd;}
}